package com.qianli.cashmama.product.server.entity;
import com.alibaba.fastjson.JSON;
import com.fqgj.common.entity.BaseEntity;
import com.fqgj.common.utils.StringUtils;
import com.qianli.cashmama.product.facade.enums.ErrorCodeEnum;
import com.qianli.cashmama.product.facade.enums.SellStatusEnum;
import com.qianli.cashmama.product.facade.result.Product;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import com.qianli.cashmama.product.facade.result.ProductConfig;
import com.qianli.cashmama.product.server.domain.ProductCategoryDomain;
import com.qianli.cashmama.product.server.domain.ProductDomain;
import com.qianli.cashmama.product.server.exception.ProductException;

import java.math.BigDecimal;
import java.util.Date;
/**
* User: anonymous
* Date: 2020-12-01
* Time: 14:43:56.852
* TableDesc:
*/
public class ProductEntity extends BaseEntity{
    /**
    *产品编码
    */
    private String code;
    /**
    *本金
    */
    private BigDecimal principalMoney;
    /**
    *实际到账金额
    */
    private BigDecimal arrivalMoney;
    /**
    *每期还款时间
    */
    private Integer paybackPeriodLength;
    /**
    *还款总期数
    */
    private Integer totalPaybackPeriodCount;
    /**
    *服务费 (service_charge = pre_service_charge + gst_charge)
    */
    private BigDecimal serviceCharge;
    /**
    *前置服务费 (service_charge = pre_service_charge + gst_charge)
    */
    private BigDecimal preServiceCharge;
    /**
    *税费 (service_charge = pre_service_charge + gst_charge)
    */
    private BigDecimal gstCharge;
    /**
    *一次性登记费用 (service_charge = pre_service_charge + gst_charge +one_time_on_boarding_fee)
    */
    private BigDecimal oneTimeOnBoardingFee;
    /**
    *逾期服务费
    */
    private BigDecimal overdueServiceCharge;
    /**
    *息费费率（个税贷 月息费 等额本息计算方式）
    */
    private BigDecimal rate;
    /**
    *罚息费率(个税贷按日收取，每日罚息=当期本金*0.2%，trade拆分成两笔0.1%入库)
    */
    private BigDecimal penaltyRate;
    /**
    *服务费率（个税贷服务费按月计算，需要设置为1%，每月服务费=借款总金额*1%）
    */
    private BigDecimal serviceFeeRate;
    /**
    *所属产品类目
    */
    private String productCategory;
    /**
    *货架类型 针对不同类目可以有不同的含义 1-默认
    */
    private Integer shelfType;
    /**
    *产品协议
    */
    private String protocol;
    /**
    *产品logo
    */
    private String logo;
    /**
    *是否上架,0否,1是
    */
    private Integer isSelling;
    /**
    *有效期开始时间
    */
    private Date startTime;
    /**
    *有效期结束时间
    */
    private Date endTime;
    /**
    *产品风险等级 1~5
    */
    private Integer riskLevel;
    /**
    *其它配置， json格式存储
    */
    private String config;
    /**
    *是否被删除,0否,1是
    */
    private Boolean isDeleted;
    /**
    *app端是否展示：0-否，1-是
    */
    private String showFlag;

    public ProductDomain toDomain(ProductCategoryDomain pcDomain) {
        ProductDomain domain = new ProductDomain();
        domain.setCode(this.getCode());
        domain.setPrincipalMoney(this.getPrincipalMoney());
        domain.setArrivalMoney(this.getArrivalMoney());
        domain.setPaybackPeriodLength(this.getPaybackPeriodLength());
        domain.setServiceCharge(this.getServiceCharge());
        domain.setPreServiceCharge(this.getPreServiceCharge());
        domain.setGstCharge(this.getGstCharge());
        domain.setOneTimeOnBoardingFee(this.getOneTimeOnBoardingFee());
        domain.setOverdueServiceCharge(this.getOverdueServiceCharge());
        domain.setRate(this.getRate());
        domain.setPenaltyRate(this.getPenaltyRate());
        domain.setServiceFeeRate(this.getServiceFeeRate());
        domain.setProductCategoryDomain(pcDomain);
        domain.setShelfType(this.getShelfType());
        domain.setProtocol(this.getProtocol());
        domain.setLogo(this.getLogo());
        domain.setTotalPaybackPeriodCount(this.getTotalPaybackPeriodCount());
        SellStatusEnum isSelling = SellStatusEnum.getByStatus(this.getIsSelling());
        if (isSelling == null) {
            throw new ProductException(ErrorCodeEnum.INVALID_SELLING_STATUS);
        }
        domain.setIsSelling(isSelling);
        domain.setStartTime(this.getStartTime());
        domain.setEndTime(this.getEndTime());
        domain.setShowFlag(this.getShowFlag());
        domain.setRiskLevel(riskLevel);
        domain.setConfig(this.getConfig());
        return domain;
    }

    public Product newFacade(ProductCategory productCategory) {
        Product product = new Product();
        product.setCode(code);
        product.setPrincipalMoney(principalMoney.toString());
        product.setArrivalMoney(arrivalMoney.toString());
        product.setPaybackPeriodLength(paybackPeriodLength);
        product.setServiceCharge(serviceCharge.toString());
        product.setPreServiceCharge(preServiceCharge.toString());
        product.setGstCharge(gstCharge.toString());
        product.setOverdueServiceCharge(overdueServiceCharge.toString());
        product.setRate(rate.toString());
        product.setPenaltyRate(penaltyRate.toString());
        product.setProductCategory(productCategory);
        product.setShelfType(shelfType);
        product.setProtocol(protocol);
        product.setLogo(logo);
        product.setTotalPaybackPeriodCount(this.getTotalPaybackPeriodCount());
        product.setShowFlag(showFlag);
        product.setRiskLevel(riskLevel);
        Date now = new Date();
        if (isSelling == SellStatusEnum.ON_SELL.getStatus() && startTime.before(now) && endTime.after(now)) {
            product.setActive(true);
        } else {
            product.setActive(false);
        }
        product.setConfig(StringUtils.isEmpty(config) ? null : JSON.parseObject(config, ProductConfig.class));
        return product;
    }

    public  String getCode(){
        return this.code;
    }

    public  ProductEntity setCode(String code){
        this.code = code;
        return this;
    }

    public  BigDecimal getPrincipalMoney(){
        return this.principalMoney;
    }

    public  ProductEntity setPrincipalMoney(BigDecimal principalMoney){
        this.principalMoney = principalMoney;
        return this;
    }

    public  BigDecimal getArrivalMoney(){
        return this.arrivalMoney;
    }

    public  ProductEntity setArrivalMoney(BigDecimal arrivalMoney){
        this.arrivalMoney = arrivalMoney;
        return this;
    }

    public  Integer getPaybackPeriodLength(){
        return this.paybackPeriodLength;
    }

    public  ProductEntity setPaybackPeriodLength(Integer paybackPeriodLength){
        this.paybackPeriodLength = paybackPeriodLength;
        return this;
    }

    public  Integer getTotalPaybackPeriodCount(){
        return this.totalPaybackPeriodCount;
    }

    public  ProductEntity setTotalPaybackPeriodCount(Integer totalPaybackPeriodCount){
        this.totalPaybackPeriodCount = totalPaybackPeriodCount;
        return this;
    }

    public  BigDecimal getServiceCharge(){
        return this.serviceCharge;
    }

    public  ProductEntity setServiceCharge(BigDecimal serviceCharge){
        this.serviceCharge = serviceCharge;
        return this;
    }

    public  BigDecimal getPreServiceCharge(){
        return this.preServiceCharge;
    }

    public  ProductEntity setPreServiceCharge(BigDecimal preServiceCharge){
        this.preServiceCharge = preServiceCharge;
        return this;
    }

    public  BigDecimal getGstCharge(){
        return this.gstCharge;
    }

    public  ProductEntity setGstCharge(BigDecimal gstCharge){
        this.gstCharge = gstCharge;
        return this;
    }

    public  BigDecimal getOneTimeOnBoardingFee(){
        return this.oneTimeOnBoardingFee;
    }

    public  ProductEntity setOneTimeOnBoardingFee(BigDecimal oneTimeOnBoardingFee){
        this.oneTimeOnBoardingFee = oneTimeOnBoardingFee;
        return this;
    }

    public  BigDecimal getOverdueServiceCharge(){
        return this.overdueServiceCharge;
    }

    public  ProductEntity setOverdueServiceCharge(BigDecimal overdueServiceCharge){
        this.overdueServiceCharge = overdueServiceCharge;
        return this;
    }

    public  BigDecimal getRate(){
        return this.rate;
    }

    public  ProductEntity setRate(BigDecimal rate){
        this.rate = rate;
        return this;
    }

    public  BigDecimal getPenaltyRate(){
        return this.penaltyRate;
    }

    public  ProductEntity setPenaltyRate(BigDecimal penaltyRate){
        this.penaltyRate = penaltyRate;
        return this;
    }

    public  BigDecimal getServiceFeeRate(){
        return this.serviceFeeRate;
    }

    public  ProductEntity setServiceFeeRate(BigDecimal serviceFeeRate){
        this.serviceFeeRate = serviceFeeRate;
        return this;
    }

    public  String getProductCategory(){
        return this.productCategory;
    }

    public  ProductEntity setProductCategory(String productCategory){
        this.productCategory = productCategory;
        return this;
    }

    public  Integer getShelfType(){
        return this.shelfType;
    }

    public  ProductEntity setShelfType(Integer shelfType){
        this.shelfType = shelfType;
        return this;
    }

    public  String getProtocol(){
        return this.protocol;
    }

    public  ProductEntity setProtocol(String protocol){
        this.protocol = protocol;
        return this;
    }

    public  String getLogo(){
        return this.logo;
    }

    public  ProductEntity setLogo(String logo){
        this.logo = logo;
        return this;
    }

    public  Integer getIsSelling(){
        return this.isSelling;
    }

    public  ProductEntity setIsSelling(Integer isSelling){
        this.isSelling = isSelling;
        return this;
    }

    public  Date getStartTime(){
        return this.startTime;
    }

    public  ProductEntity setStartTime(Date startTime){
        this.startTime = startTime;
        return this;
    }

    public  Date getEndTime(){
        return this.endTime;
    }

    public  ProductEntity setEndTime(Date endTime){
        this.endTime = endTime;
        return this;
    }

    public  Integer getRiskLevel(){
        return this.riskLevel;
    }

    public  ProductEntity setRiskLevel(Integer riskLevel){
        this.riskLevel = riskLevel;
        return this;
    }

    public  String getConfig(){
        return this.config;
    }

    public  ProductEntity setConfig(String config){
        this.config = config;
        return this;
    }

    public  Boolean getIsDeleted(){
        return this.isDeleted;
    }

    public  ProductEntity setIsDeleted(Boolean isDeleted){
        this.isDeleted = isDeleted;
        return this;
    }

    public  String getShowFlag(){
        return this.showFlag;
    }

    public  ProductEntity setShowFlag(String showFlag){
        this.showFlag = showFlag;
        return this;
    }
}
